﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using FairyGUI;
using DG.Tweening;
using Project;
using Project.UI;
using XLua;

namespace Project.controller
{
    [LuaCallCSharp]
    public class NavigationController : MonoBehaviour
    {
        void Awake()
        {
            if (Instance == null)
            {
                Instance = this;
            }
        }


        static public NavigationController Instance { get; private set; }

        private Stack<ViewController> _Views = new Stack<ViewController>();
        public Stack<ViewController> Views
        {
            get { return _Views; }
            set { _Views = value; }
        }

        public ViewController TopViewController
        {
            get
            {
                return _Views.Count > 0 ? _Views.Peek() : null;
            }
        }

        public void PushViewController(ViewController viewController)
        {
            
            var top = TopViewController;

            if (top != null)
            {
                Debug.Log("top.visible:" + top.visible);
                top.visible = false;
            }

            Views.Push(viewController);

            GRoot.inst.AddChild(viewController);
            Debug.Log("Views.Count:"+Views.Count);

        }


        public GComponent PopViewController()
        {
            
            var pop = _Views.Pop();
            Debug.Log("pop:"+pop);
            pop.DismissViewController(true, null, true);
            //GRoot.inst.RemoveChild(pop);
            var top = TopViewController;
            GameManager.currentView = top;
            if (top != null)
            {
                top.visible = true;
                
            }
            //Debug.Log("PopViewController--------Views.Count:" + Views.Count + "-----Views:" + GameManager.currentView.GetType());
            return pop;
        }

        public void ClearnView()
        {
            if (_Views.Count > 0)
            {
                for (int i=0; i < _Views.Count; i++)
                {
                    PopViewController();
                }

                 _Views.Clear();
            }
            Debug.Log("_Views.Count:"+_Views.Count);

        }
    }
}
